package independence.events
{
	import __AS3__.vec.Vector;
	import com.carlcalderon.arthropod.Debug;
	
	import flash.events.EventDispatcher;
	
	import independence.reflection.ClassLoader;
	
	/**
	 * <code>EventManager</code>, when used as a base class in a <i>is a</i> relationship or when
	 * used in composition in a <i>has a</i> relationship, allows objects to have explicit control
	 * over the event listeners currently registered to them. <code>EventManager</code> defines methods
	 * that allow you to easily enable and disable all event listeners on an instance, as well as 
	 * delete all listeners altogether. 
	 * 
	 * @author	TK, TK Assembled for Independence Glory. 
	 */
	public class EventManager extends EventDispatcher implements IEventManager
	{
		/**
		 * Holds all <code>EventListener</code> instances listening to this <code>EventManager</code>.
		 */
		private var eventList:Vector.<EventListener> = new Vector.<EventListener>();
		/**
		 * Constructs a new <code>EventManager</code> instance.
		 */
		public function EventManager()
		{
			
		}
		/**
		 * @inheritDoc
		 */
		public function addEventListeners(listeners:Vector.<EventListener>):void
		{
			for each (var listener:EventListener in listeners) {
				this.addEventListener(listener.type, listener.listener, listener.useCapture, listener.priority, listener.useWeakReference);
			}
		}
		/**
		 * @inheritDoc
		 */
		public function clearEventListeners():void
		{
			for each (var listener:EventListener in this.eventList) {
				this.removeEventListener(listener.type, listener.listener, listener.useCapture);
			}
			this.eventList = new Vector.<EventListener>();
		}
		/**
		 * @inheritDoc
		 */
		public function disableEventListeners():void
		{
			for each (var listener:EventListener in this.eventList) {
				super.removeEventListener(listener.type, listener.listener, listener.useCapture);
			}
		}
		/**
		 * @inheritDoc
		 */
		public function enableEventListeners():void
		{
			for each (var listener:EventListener in this.eventList) {
				super.addEventListener(listener.type, listener.listener, listener.useCapture, listener.priority, listener.useWeakReference);
			}
		}
		/**
		 * @inheritDoc
		 */
		override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			super.addEventListener(type, listener, useCapture, priority, useWeakReference);
			var eventListener:EventListener = new EventListener();
			eventListener.setup(type, listener, useCapture, priority, useWeakReference);
			this.eventList.push(eventListener);
		}
		/**
		 * @inheritDoc
		 */
		override public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
		{
			super.removeEventListener(type, listener, useCapture);
			for (var i:uint = 0; i < this.eventList.length; i++) {
				var eventListener:EventListener = this.eventList[i];
				if (eventListener.type == type && eventListener.listener == listener
						&& eventListener.useCapture == useCapture) {
					this.eventList.splice(i, 1);			
				}
			}
		}
		/**
		 * @inheritDoc
		 */
		public function getEventListeners():Vector.<EventListener> 
		{
			return this.eventList;
		}
		/**
		 * Returns this object's <code>Class</code> object.
		 * @return	Returns this object's class.
		 */
		public function getClass():Class
		{
			return ClassLoader.getClass(this);
		}
		
		public override function toString():String
		{
			return "[EventManager object]";
		}
	}
}